Skip to content

fix: The interface for obtaining model metadata cannot access public models#2787

Merged
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_model
Apr 2, 2025
Merged

fix: The interface for obtaining model metadata cannot access public models#2787
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_model

Conversation

@shaohuzhang1
Copy link
Copy Markdown
Contributor

fix: The interface for obtaining model metadata cannot access public models

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Apr 2, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Apr 2, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

model = QuerySet(Model).get(id=self.data.get('id'))
return {'id': str(model.id), 'provider': model.provider, 'name': model.name, 'model_type': model.model_type,
'model_name': model.model_name,
'status': model.status,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code snippet has a few potential issues and optimizations:

  1. Duplicate if block:

    if model is None:
        model = QuerySet(Model).get(id=self.data.get('id'))

    This duplicated if condition can be removed. It's already covered by the outer if with_valid: .... If with_valid is true, it will have checked for existence earlier.

  2. String comparison efficiency:

    if str(model.user_id) != str(self.data.get("user_id")):

    String comparisons like this should be avoided unless absolutely necessary because they involve type conversion. Instead, you could compare the actual values directly:

    if model.user_id != self.data.get("user_id"):
  3. Code readability:

    • The code uses different spaces between operators (==, !=) which might look inconsistent.
    • Adding consistent spacing can improve readability.
  4. Redundant checks:

    if model is None:
        raise AppApiException(500, _('Model does not exist')

    You don't always need to raise an exception when a model doesn't exist; simply returning None or handling it gracefully might be better depending on its context.

Here’s a revised version of the function with these improvements:

@@ -313,18 +313,21 @@ def one(self, with_valid=False):
             return ModelSerializer.model_to_dict(model)

         def one_meta(self, with_valid=False):
             if with_valid:
                 super().is_valid(raise_exception=True)
                 model_id = self.data.get('id')
                 try:
                     model = Model.objects.filter(id=model_id).first()
                     if not model:
                         raise AppApiException(500, _('Model does not exist'))
                     if model.permission_type == 'PRIVATE' and model.user_id != self.data.get("user_id"):
                         raise Exception(_('No permission to use this model') + f" ({model.name})")
                 except Exception as e:
                     handle_exception(e)
                 model_id = self.data.get('id')
                 
             if isinstance(model_id, int):  # Ensure model_id is an integer
                 model = Model.objects.get(id=model_id)
             else:
                 raise ValueError(_('Invalid model ID'))

             return {
                 'id': str(model.id),
                 'provider': model.provider,
                 'name': model.name,
                 'model_type': model.model_type,
                 'model_name': model.model_name,
                 'status': model.status
             }

Key points:

  • Removed unnecessary duplicate if blocks.
  • Simplified string comparison by avoiding type conversion unless needed.
  • Added additional logic to ensure that self.data['id'] is an integer before calling QuerySet.get().
  • Improved overall code readability and maintainability through consistent formatting and proper separation of concerns.

@shaohuzhang1 shaohuzhang1 merged commit 9d6451b into main Apr 2, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@fix_model branch April 2, 2025 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant